<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-05-17 11:01
 *
 * 项目：upload  -  $  - FormsTable.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\table;

use Lev;
use lev\base\Modelv;
use lev\base\SubModelv;
use lev\helpers\SettingsHelper;
use modules\levfm\helpers\fmUrlHelper;
use modules\levfm\table\form\FormData\fieldValidations;
use modules\levfm\widgets\inputs\inputsFormWidget;

!defined('INLEV') && exit('Access Denied LEV');


class LevfmFields extends SubModelv
{

    public static $tableName = '{{%levfm_fields}}';

    public static function settings__tmp_field()
    {
        return [
            '默认模板',
            '投票模板（仅限select下拉性字段）'
        ];
    }

    public static function copyOne($id = null)
    {
        $rs = parent::copyOne($id); // TODO: Change the autogenerated stub
        if (!empty($rs['inid'])) {
            static::update(['inputname'=>LevfmForms::tableFieldname($rs['inid']), 'status'=>1], ['id'=>$rs['inid']]);
        }
        return $rs;
    }

    public static function findAllKeyInputname($where, $order = [], $field = '*')
    {
        $_fields = static::findAll($where, '', $order, $field);
        return static::setFieldInputname($_fields);
    }
    public static function setFieldInputname($_fields) {
        $fields = [];
        foreach ($_fields as $v) {
            $v['inputname'] = $v['inputname'] ?: LevfmForms::tableFieldname($v['id']);
            $fields[$v['inputname']] = $v;
        }
        return $fields;
    }

    public static function tableFieldname($id) {
        return LevfmForms::tableFieldname($id);
    }

    public static function getfmFormSettings($formid) {
        $_GET['formid'] = $formid;//传递给selectscode -> srhType
        return SettingsHelper::findAll(['classify'=>'form', 'moduleidentifier'=> Lev::$app['iden']]);
    }

    public static function setsrhtype() {
        $formid = floatval(Lev::GPv('formid'));
        return static::getTypeSearchs($formid);
    }

    /**
     * 根据formid缓存已经查询出来的表单字段
     * @param $formid
     * @return mixed
     */
    public static function getFormidFields($formid) {
        static $formids;
        if (!isset($formids[$formid])) {
            $formids[$formid] = static::findAll(['formid' => $formid], '', ['displayorder ASC', 'id ASC']);
        }
        return $formids[$formid];
    }

    /**
     * 提取分类搜索字段
     * @param $formid
     * @return array
     */
    public static function getTypeSearchs($formid) {
        $arr = [];
        if ($formid >0) {
            $fields = static::getFormFields($formid);
            foreach ($fields as $v) {
                static::isSrhtypeField2($v) && $arr[$v['inputname']] = $v['title'];
            }
        }
        return $arr;
    }

    public static function isSrhtypeField($inputtype) {
        return stripos($inputtype, 'select') !== false;
    }
    public static function isSrhtypeField2($inputInfo, $ckOption = false)
    {
        if (static::isSrhtypeField($inputInfo['inputtype']) && ($options = inputsFormWidget::getSelectItem($inputInfo))) {
            if ($ckOption !== false) {
                if (is_array($ckOption)) {
                    foreach ($ckOption as $k => $v) {
                        if (!isset($options[$v])) unset($ckOption[$k]);
                    }
                    return $ckOption;
                }else {
                    return isset($options[$ckOption]) ? $ckOption : false;
                }
            }
            return true;
        }
        return false;
    }

    public static function getRules($rules, $settings = null)
    {
        $arr = Lev::getSettings($rules)[0];
        if ($settings) {
            $settings = Lev::getSettings($settings);
            foreach ($settings as $k => $v) {
                ($ruleKey = fieldValidations::getSettingRuleKey($k)) && $arr[$ruleKey] = $v;
            }
        }
        return $arr;
    }

    public static function setBtn($id)
    {
        return Lev::GETv('preview') ? '<a class="openPP field-set-btn" href="'.fmUrlHelper::addField(0, $id).'"><svg class="icon"><use xlink:href="#fa-set"></use></svg></a>' : '';
    }

    public static function getFormFields($formid)
    {
        return static::setFieldInputname(static::getFormidFields($formid));
    }

    public static function getFieldTmp($settings)
    {
        return floatval(Lev::getSettings($settings, 'tmp_field'));
    }

    public static function isVoteTmp($settings)
    {
        return static::getFieldTmp($settings) == '1';
    }


}